<template>
  <div class="me-message" v-title >
    <el-container class="me-view-container">
      <el-main class="me-main">
        <div class="me-view-comment">
          <div class="me-view-comment-write">
            <el-row :gutter="20">
              <el-col :span="2">
                <!--<a class="">
                  <img class="me-view-picture" :src="avatar" />
                </a>-->
              </el-col>
              <el-col :span="21">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 2 }"
                  placeholder="你的留言..."
                  class="me-view-comment-text"
                  v-model="comment.content"
                  resize="none"
                >
                </el-input>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="3" :offset="21">
                <el-button
                  type="text"
                  @click="addComment"
                  style="margin-top: 5px"
                >留言</el-button
                >
              </el-col>
            </el-row>
          </div>

          <div class="me-view-comment-title">
            <span> {{ commentCounts }}条留言</span>
          </div>

          <commment-item
            v-for="(c, index) in comments"
            :comment="c"
            :index="index"
            :userId="comment.userId"
            :childComments="c.childComments"
            :rootCommentCounts="comments.length"
            @commentCountsIncrement="commentCountsIncrement"
            :key="c.id"
          >
          </commment-item>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  import CommmentItem from "@/components/comment/CommentItem";
  import { getMessage, addMessage } from "@/api/comment";
  import { Message } from "element-ui";
  import default_avatar from "@/assets/img/default_avatar.png";
  export default {
    name: 'MessageBoard',
    data() {
      return {
        comments: [],
        comment: {
          content: "",
          userId: 0
        },
        commentCounts:0
      }
    },
    components: {
      CommmentItem,
    },
    computed: {
      title() {
        return '留言板';
      },
    },
    methods: {
      addComment() {
          let that = this;
          if (!that.comment.content) {
            return;
          }
          this.comment.userId = this.$store.getters.getUser.id;
          addMessage(that.comment)
            .then((data) => {
              that.$message({
                type: "success",
                message: "留言成功",
                showClose: true,
              });
              that.comments.unshift(data.data);
              that.commentCountsIncrement();
              that.comment.content = "";
              //this.getCommentsByBlogId();
            })
            .catch((error) => {
              if (error !== "error") {
                that.$message({
                  type: "error",
                  message: "评论失败",
                  showClose: true,
                });
              }
            });

      },

      commentCountsIncrement() {
        this.commentCounts += 1;
      },
    },
    mounted () {
      getMessage().then((data) => {
        this.comments = data.data;
        this.commentCounts = this.comments.length
      })
    }
  }
</script>

<style>
  .me-view-body {
    margin: 100px auto 140px;
  }

  .me-view-container {
    width: 1000px;
  }

  .el-main {
    overflow: hidden;
  }

  .me-view-title {
    font-size: 34px;
    font-weight: 700;
    line-height: 1.3;
  }

  .me-view-author {
    /*margin: 30px 0;*/
    margin-top: 30px;
    vertical-align: middle;
  }

  .me-view-picture {
    width: 40px;
    height: 40px;
    border: 1px solid #ddd;
    border-radius: 50%;
    vertical-align: middle;
    background-color: #5fb878;
  }

  .me-view-info {
    display: inline-block;
    vertical-align: middle;
    margin-left: 8px;
  }

  .me-view-meta {
    font-size: 12px;
    color: #969696;
  }

  .me-view-end {
    margin-top: 20px;
    padding-right: 25px;
  }

  .me-view-tag {
    margin-top: 20px;
    padding-left: 6px;
    border-left: 4px solid #c5cac3;
    position: relative;
  }
  .dianzan {
    position: absolute;
    right: 20px;
    top: 5px;
  }

  .me-view-tag img {
    width: 30px;
    height: auto;
  }

  .me-view-tag-item {
    margin: 0 4px;
  }

  .me-view-comment {
    margin-top: 60px;
  }

  .me-view-comment-title {
    font-weight: 600;
    border-bottom: 1px solid #f0f0f0;
    padding-bottom: 20px;
  }

  .me-view-comment-write {
    margin-top: 20px;
  }

  .me-view-comment-text {
    font-size: 16px;
  }

  .v-show-content {
    padding: 8px 25px 15px 0px !important;
  }

  .v-note-wrapper .v-note-panel {
    box-shadow: none !important;
  }

  .v-note-wrapper .v-note-panel .v-note-show .v-show-content,
  .v-note-wrapper .v-note-panel .v-note-show .v-show-content-html {
    background: #fff !important;
  }
  .me-view-card {
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 100%;
    min-height: 700px;
    margin: 10px auto;
    padding: 20px 15px;
  }
</style>
